
$(document).ready(function(){
	loadOrgsData();
});

var orgZTree,parentZTree,orgRMenu;
var orgTreeSetting = {	
		data:{
			key:{
				name:"name"
			},
			simpleData:{
				enable:true,
				idKey:"orgId",
				pIdKey:"parentOrgId",
				rootPId:0
			}
		},
		callback:{
			onClick:loadOrgByOrgId,
			onRightClick:rightClick
		}
	};
	/**
	 * 点击选择按钮加载组织树
	 */
	var setting = {
		view: {
			dblClickExpand: false
		},
		data:{
			key:{
				name:"name"
			},
			simpleData:{
				enable:true,
				idKey:"orgId",
				pIdKey:"parentOrgId",
				rootPId:0
			}
		},
		callback: {
			onClick:loadOrgByOrgId,
			onClick: onClick
		}
	};
	
	function onClick(e, treeId, treeNode) {
		var orgName = treeNode.name;
		var orgId = treeNode.orgId;
		var orgObj = $("#parentOrgName");
		orgObj.attr("value", orgName);
		$("#parentOrgId").val(orgId);
		hideMenu();
	}

	function showMenu() {
		var url = appCtx + "secure/org/loadOrgsData.html";
		$.post(url,function(data){ 
			parentZTree = $.fn.zTree.init($("#showTree"), setting, data);
		});
		
		var orgObj = $("#parentOrgName");
		var orgOffset = $("#parentOrgName").offset();
		$("#menuContent").css({left:orgOffset.left + "px", top:(orgOffset.top + orgObj.outerHeight()-76) + "px"}).show();
		$("body").bind("mousedown", onBodyDown);		
	}
	
	function hideMenu() {
		$("#menuContent").fadeOut("fast");
		$(".orgView").unbind("mousedown", onBodyDown);
	}
	
	function onBodyDown(event) {
		if (!(event.target.id == "menuBtn" || event.target.id == "menuContent" || $(event.target).parents("#menuContent").length>0)) {
			hideMenu();
		}
	}
	
	/**
	 * 加载模块树形数据
	 */
	function loadOrgsData(){
		var url = appCtx + "secure/org/loadOrgsData.html";
		$.post(url,function(data){ 
			orgZTree=$.fn.zTree.init($("#orgTree"), orgTreeSetting, data);
		});
		orgRMenu = $("#orgRMenu");
	}

	function loadOrgByOrgId(event,treeId,treeNode){
		var orgId = treeNode.orgId;
		refreshOrgBody(orgId);
	}

	function refreshOrgBody(orgId){
		var url = appCtx + "secure/org/queryOrgByOrgId.html";
		$.post(url,{orgId:orgId},function(data){ 
			$("#orgBody").html(data);
		});
	}
	
	function rightClick(event, treeId, treeNode){
		if (!treeNode && event.target.tagName.toLowerCase() != "button" && $(event.target).parents("a").length == 0) {
			orgZTree.cancelSelectedNode();
			showOrgRMenu("root", event.clientX, event.clientY);
		} else if (treeNode && !treeNode.noR) {
			orgZTree.selectNode(treeNode);
			showOrgRMenu("node", event.clientX, event.clientY);
		}
	}

	function showOrgRMenu(type, x, y) {
		y=y-78;
		$("#orgRMenu ul").show();
		$("#addOrg").show();
		$("#delOrg").show();
		$("#order").show();
		orgRMenu.css({"top":y+"px", "left":x+"px", "visibility":"visible"});
		$(".orgView").bind("mousedown", onOrgBodyMouseDown);
	}

	function addOrg(){
		hideOrgRMenu();
		var url = appCtx + "secure/org/addOrg.html";
		var nodes = orgZTree.getSelectedNodes();
		if(nodes[0]){
			var parentOrgId = nodes[0].orgId;
			$.post(url,{parentOrgId:parentOrgId},function(data){ 
				$("#orgBody").html(data);
			});
		}
	}

	function delOrg(){
		hideOrgRMenu();
		var url = appCtx + "secure/org/delete.html";
		var nodes = orgZTree.getSelectedNodes();
		if(nodes[0]){
			var msg = "";
			var children = nodes[0].children;
			if(children && children.length>0){
				msg = "存在子组织,不允许删除!";
				alert(msg);
				return;
			}
			var orgId = nodes[0].orgId;
			var parentOrgId = nodes[0].parentOrgId;
			var name = nodes[0].name;
			msg = "确认删除组织［"+name+"］吗?";
			if(confirm(msg)==true){
				$.post(url,{orgId:orgId},function(data){ 
					$(".alert").css("visibility","visible").addClass("alert-success").html("删除成功!").fadeOut(3000);
					window.setTimeout(function(){
						refreshOrgBody(parentOrgId);
					}, 2000);
					orgZTree.removeNode(nodes[0]);
				});
			}
		}
	}

	function hideOrgRMenu() {
		if (orgRMenu) orgRMenu.css({"visibility": "hidden"});
		$(".orgView").unbind("mousedown", onOrgBodyMouseDown);
	}
	function onOrgBodyMouseDown(event){
		if (!(event.target.id == "orgRMenu" || $(event.target).parents("#orgRMenu").length>0)) {
			orgRMenu.css({"visibility" : "hidden"});
		}
	}

	function checkTreeNode(checked) {
		var nodes = orgZTree.getSelectedNodes();
		if (nodes && nodes.length>0) {
			orgZTree.checkNode(nodes[0], checked, true);
		}
		hideOrgRMenu();
	}

	function resetTree() {
		hideOrgRMenu();
		loadOrgsData();
	}
	/**
	function expandNode(treeNode, expandFlag, sonSign, focus, callbackFlag){
		var treeObj = $.fn.zTree.getZTreeObj("orgZTree");
		var nodes = treeObj.getSelectedNode();
		if (nodes.length>0) {
			treeObj.expandNode(nodes[0], true, true, true);
		}
	}
	*/

	/**
	 * 保存模块
	 */
	function saveOrg(){
		// 验证
		var flag = $("#orgForm").validationAjax();
		if(flag){
			// 提交
			var url = appCtx + "secure/org/save.html";
			var params = $("#orgForm").serialize();
			$.post(
				url,
				params,
				function(result){
					var flag = result.flag;
					var org = result.org;
					var msg = "";
					if(flag=="success"){
						msg="保存成功!";
						$(".alert").css("visibility","visible").addClass("alert-success").html(msg);
						window.setTimeout(function(){
							refreshOrgBody(org.orgId);
						}, 2000);
						var node = orgZTree.getNodeByParam("orgId", org.parentOrgId, null);
						var currentNode = orgZTree.getNodeByParam("orgId", org.orgId, null);
						if(currentNode){
							currentNode.name=org.name;
							orgZTree.updateNode(currentNode);
						}else{
							orgZTree.addNodes(node,org);
						}
					}else if(flag=="error"){
						msg="保存失败!";
						$(".alert").css("visibility","visible").addClass("alert-error").html(msg);
						window.setTimeout(function(){
							$(".alert").css("visibility","hidden").removeClass("alert-error");
						}, 2000);
					}
				}
			);
		}
		return flag;
	}
	
	
	

	

	